package com.duoduo.demo.springboot;

import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import com.duoduo.demo.springboot.interceptor.MyInterceptor1;
import com.duoduo.demo.springboot.interceptor.MyInterceptor2;

/**
 * 拦截器 @url http://blog.csdn.net/catoop/article/details/50501696
 * @author chengesheng@gmail.com
 * @date 2017年11月16日下午3:59:19
 */
// @Configuration
public class MyWebAppConfigurer extends WebMvcConfigurationSupport {

	/**
	 * 增加静态资源处理
	 */
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		// 访问myres根目录下的fengjing.jpg 的URL为 http://localhost:8080/fengjing.jpg （/** 会覆盖系统默认的配置）
		// registry.addResourceHandler("/**").addResourceLocations("classpath:/myres/").addResourceLocations("classpath:/static/");

		// addResourceLocations 的参数是动参，多个地址可以写成
		// registry.addResourceHandler("/**").addResourceLocations("classpath:/myres/","classpath:/static/");

		// 可以直接使用addResourceLocations 指定磁盘绝对路径，同样可以配置多个位置，注意路径写法需要加上file:
		// registry.addResourceHandler("/myimgs/**").addResourceLocations("file:H:/myimgs/");

		registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
		super.addResourceHandlers(registry);
	}

	/**
	 * 增加拦截器
	 */
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		// 多个拦截器组成一个拦截器链
		// addPathPatterns 用于添加拦截规则
		// excludePathPatterns 用户排除拦截
		registry.addInterceptor(new MyInterceptor1()).addPathPatterns("/**");
		registry.addInterceptor(new MyInterceptor2()).addPathPatterns("/**");
		super.addInterceptors(registry);
	}

}
